#!/usr/bin/env python
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import numpy as np
import scipy.special as sps
import random
from sets import Set
import subprocess
#{node: [year, update, social_I, social, 
#        time, people, complaint, video_I, load, contact]}
N = 875713
param_dict = {}
all_set = Set()
def init_dict(input_file):
    global all_set, param_dict
    with open(input_file, 'r+') as f:
        for line in f:
            if '#' in line:
                continue
            from_node = int(line.strip().split('\t')[0])
            to_node = int(line.strip().split('\t')[1])
            all_set.add(from_node)
            all_set.add(to_node)
    f.close()    
    for node in all_set:
        param_dict[node] = []

def normal_distribution(mu_p, sigma_p, title, img_name):
    global N, param_dict
    mu, sigma = mu_p, sigma_p # mean and standard deviation
    s = np.random.normal(mu, sigma, N)
    
    i = 0
    for k, v in param_dict.iteritems():
        v.append(abs(int(s[i])))
        i += 1
    print 'max: %d, min: %d' % (int(max(s)), int(min(s)))
    
    fig = plt.figure()
    count, bins, ignored = plt.hist(s, 30, normed=True)
    plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *
             np.exp( - (bins - mu)**2 / (2 * sigma**2) ),
            linewidth=2, color='r')
    #plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$')
    plt.title(title)
    fig.savefig('/home/ivy/git/BD_py/PageRank_py/project/resource/img/%s.png' % img_name, \
            bbox_inches='tight')
#     plt.show()

def random_8_2(value_8, value_2):    
    # 1 or 0, #of value_8 is more than #of value_2
    global param_dict, N
    shape, scale = 1., 1. # mean and dispersion
    s = np.random.random_sample((N,))
    i = 0
    for k, v in param_dict.iteritems():
        v.append(value_8 if s[i] < 0.8 else value_2)
        i += 1

def gamma_distribution(shape_p, scale_p, title, img_name):
    global param_dict, N, n
    shape, scale = shape_p, scale_p # mean and dispersion
    s = np.random.gamma(shape, scale, N)
    
    i = 0
    for k, v in param_dict.iteritems():
        v.append(int(s[i]))
        i += 1
    print 'max: %d, min: %d' % (int(max(s)),int(min(s)))
    
    fig = plt.figure()
    count, bins, ignored = plt.hist(s, 50, normed=True)
    y = bins**(shape-1)*(np.exp(-bins/scale) /
                     (sps.gamma(shape)*scale**shape))
    plt.plot(bins, y, linewidth=2, color='r')
    plt.title(title)
    
    fig.savefig('/home/ivy/git/BD_py/PageRank_py/project/resource/img/%s.png' % img_name, \
            bbox_inches='tight')
#     plt.show()

def put_social_I(num):
    #all 0 or all 1
    for key, value in param_dict.iteritems():
        value.append(num)

def put_load_gamma(title, img_name):
    global param_dict, N, n
    shape, scale = 1., .5 # mean and dispersion
    s = np.random.gamma(shape, scale, N)
    
    i = 0
    for key, value in param_dict.iteritems():
        value.append(multiply_by_10(s[i]))
        i += 1
    print 'max: %d, min: %d' % (int(max(s)),int(min(s)))
        
    fig = plt.figure()
    count, bins, ignored = plt.hist(s, 50, normed=True)
    y = bins**(shape-1)*(np.exp(-bins/scale) /
                     (sps.gamma(shape)*scale**shape))
    plt.plot(bins, y, linewidth=2, color='r')
    plt.title(title)
    
    fig.savefig('/home/ivy/git/BD_py/PageRank_py/project/resource/img/%s.png' % img_name, \
            bbox_inches='tight')

def multiply_by_10(num):
    if num < 0.1:
        num = multiply_by_10(num*10)
    return num
        
def output(output_file):
    global param_dict
    f = open(output_file, 'w+')
    f.write('#node\tyear day I   social  time  people comp.  video  load      contact\n')
    for key, value in param_dict.iteritems():
        f.write(str(key) + '\t' + '\t'.join(str(i) for i in value) + '\n')

def sort(source_file, target_file, col):
    sort = subprocess.Popen("sort -t '\t' -k%d -nr " % col, shell=True,
                    stdin=open(source_file, 'r'),
                    stdout=open(target_file, 'w'),  # Not a pipe here,
                    )

def main():
    input_file = '/home/ivy/git/BD_py/PageRank_py/project/resource/web-Google.txt'
    output_file = '/home/ivy/git/BD_py/PageRank_py/project/resource/param.txt' 
    sort_file = '/home/ivy/git/BD_py/PageRank_py/project/resource/param_sort.txt' 
    #init map
    print 'init...'
    init_dict(input_file)
    #year: 0-20 normal distribution
    print 'year...'
    normal_distribution(10, 3.16, 'Year of Establishment Distribution', 'year_of_establishment')
    #update: 0-32 normal distribution
    print 'updating period...'
    normal_distribution(16, 4, 'Updating Period Distribution', 'updating_period')
    #social_I: all 0 or all 1
    print 'social_I...'
    put_social_I(1)
    #social: #0-50 normal distribution
    print 'social...'
    normal_distribution(25, 5, 'Social Related Distribution', 'social')
    #time: 0-10K normal distribution
    print 'visiting time...'
    normal_distribution(5000, 400, 'Visiting Time Distribution', 'visiting_time')
    #pelple: 0-5000 normal distribution
    print 'visiting pelple...'
    normal_distribution(2500, 200, 'Visiting People Distribution', 'visiting_people')
    #complaint: 
    print 'complaint...'
    gamma_distribution(2., 2., 'Complaint Distribution', 'complaint')
    #not video: 1 or 0, #of 1 is more than #of 0
    print 'not video...'
    random_8_2(1, 0)
    #loading time:
    print 'loading...time'
    put_load_gamma('Loading Time Distribution', 'load_time')
    #contact: 1 or 0, #of 1 is more than #of 0
    print 'contact...'
    random_8_2(1, 0)
    print 'output...'
    output(output_file)
    print 'sort...'
    sort(output_file, sort_file, 2)
    
if __name__ == '__main__':
    main()